package com.eric.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.eric.bean.CodeBlockDetail;
import com.eric.dao.CodeBlockDetailMapper;
import org.springframework.stereotype.Service;

import com.eric.bean.CodeBlock;
import com.eric.dao.CodeBlockMapper;
import com.eric.service.CodeBlockService;

@Service("CodeBlockService")
public class CodeBlockServiceImpl implements CodeBlockService {
	
	@Resource
	private CodeBlockMapper codeBlockDao;

	@Resource
	private CodeBlockDetailMapper codeBlockDetailDao;

	@Override
	public CodeBlock getCodeBlockById(int blockId) {
		return codeBlockDao.selectByPrimaryKey(blockId);
		
	}

	@Override
	public int doAdd(HttpServletRequest request, CodeBlock block) {
//		先保存代码明细
//		明细名称
		String[] banKuaiName = request.getParameterValues("banKuaiName");
//		明细形成规则
		String[] guiZeText = request.getParameterValues("guiZeText");
//		明细值
		String[] banKuaiText = request.getParameterValues("banKuaiText");
		System.out.println(block.getSourceCode()+"---------"+block.getDetailSort()+"**************"+request.getParameter("detailSort"));
		codeBlockDao.insert(block);
		block = codeBlockDao.selectByName(block.getBlockName());
		System.out.println("数据长度"+banKuaiName.length);
		//遍历明细对象
		for (int i = 0; i < banKuaiName.length; i ++){
			String detailType = request.getParameter(banKuaiName[i]);
			CodeBlockDetail detail = new CodeBlockDetail();
			detail.setBlockID(String.valueOf(block.getId()));
			detail.setDetailName(banKuaiName[i]);
			detail.setDetailContent(banKuaiText[i]);
			detail.setDetailType(detailType);
			detail.setRule(guiZeText[i]);
//			detail.setRemark(remark[i]);
//			System.out.println("------------------------");
			codeBlockDetailDao.insert(detail);

		}

		return 1;
	}

	@Override
	public int delete(String id) {
		
		return codeBlockDao.delete(id);
	}

	@Override
	public int update(CodeBlock block) {
		
		return codeBlockDao.update(block);
	}

	@Override
	public List<Map> getList() {
		
		return codeBlockDao.selectList();
	}

	@Override
	public CodeBlock selectByName(String name){

		return codeBlockDao.selectByName(name);
	};
	@Override
	public List<Map> selectListByName(String name){
		return codeBlockDao.selectListByName(name);
	}

}
